package ca.uwindsor.cs.deepweb.estimation.method.border;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;

import ca.uwindsor.cs.deepweb.estimation.FilesContentIndexer;
import ca.uwindsor.cs.deepweb.estimation.RandomWordSelector;

public class CalculateDABSizeRemoveCorrelation {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length != 3) {
			System.out
					.println("java -jar TermsAndFrequencyExtractor.jar indexDir outputFile");
			System.exit(0);
		}
		try {
			IndexReader reader = IndexReader.open(args[0]);
			System.out.println("NumDocs = " + reader.numDocs());

			int sizeDAB = 0;

			RandomWordSelector selector = new RandomWordSelector(
					args[1], "UTF-8");
			ArrayList<String> querypoolA = selector.getWordList();
			ArrayList<String> wordsA = new ArrayList<String>(querypoolA.size());
			for (int j = 0; j < querypoolA.size(); j++) {
				wordsA.add(querypoolA.get(j).toLowerCase());
			}
			Set<Integer> IDfromA = new HashSet<Integer>();
			// QPA.addAll(wordsA);

			selector = new RandomWordSelector(
					args[2], "UTF-8");
			ArrayList<String> querypoolB = selector.getWordList();
			ArrayList<String> wordsB = new ArrayList<String>(querypoolB.size());
			for (int j = 0; j < querypoolB.size(); j++) {
				wordsB.add(querypoolB.get(j).toLowerCase());
			}
			Set<Integer> IDfromB = new HashSet<Integer>();
			// QPB.addAll(wordsB);

			IndexSearcher searcher = new IndexSearcher(args[0]);
			Query query = null;
			Hits hits = null;
			QueryParser queryparser = new QueryParser(
					FilesContentIndexer.FIELD_CONTENT, new StandardAnalyzer());
			int i, j;

			for (i = 0; i < querypoolA.size(); i++) {
				query = queryparser.parse(querypoolA.get(i));
				hits = searcher.search(query);

				for (j = 0; j < hits.length(); j++) {
					IDfromA.add(hits.id(j));
				}
			}
			System.out.println(IDfromA.size());

			for (i = 0; i < querypoolB.size(); i++) {
				query = queryparser.parse(querypoolB.get(i));
				hits = searcher.search(query);

				for (j = 0; j < hits.length(); j++) {
					IDfromB.add(hits.id(j));
				}
			}
			System.out.println(IDfromB.size());

			IDfromB.retainAll(IDfromA);
			sizeDAB = IDfromB.size();

			reader.close();

			System.out.println(sizeDAB);
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
